module service.LogInService;

import std.stdio;
import repository;
import model;
import client.MsgServerClient;
import msgtrans;
import hunt.logging;
import message.Constants;
import message.MsgDefine;
import std.conv : to;
import service.IdGeneratorService;
import google.protobuf;
import std.array;
import core.stdc.time;
import std.stdio;

class LogInService {

    static message.MsgDefine.User processLogin(message.MsgDefine.User u)
    {
        // User user = null;
        auto repo = new UserRepository();
        auto friendRepo = new FriendApplyRepository;
        bool exist = false;
        uint id;

        // user exist in db
        if (repo.findById(u.id.to!int))
        {
            exist = true;
        }

        if (!repo.flushUser(u))
        {
            logErrorf("Write user data error, uid: %s", u.id);
            std.stdio.stdout.flush();
            return null;
        }

        if(!exist)
        {
            //id = IdGeneratorService.instance().getNextId();
            FriendApply friendApply = new FriendApply;
            friendApply.userId = to!int(u.id);
            friendApply.friendId = to!int(u.id);
            friendApply.status = 1;
            friendApply.src = 0;

            id = friendRepo.putApplyContactReq(friendApply);

            auto f = new FriendRepository;
            f.putSelf(u.id, to!string(id));

            ChatMapAction chatMapAction = new ChatMapAction;
            chatMapAction.chatId = to!string(id);
            chatMapAction.type = ChatMapActionType.CREATE;
            chatMapAction.manager = u.id;
            chatMapAction.member ~= u.id;
            chatMapAction.timestamp = core.stdc.time.time(null);
            auto msgClient =  MsgServerClient.getInstance().getForwardClient(to!int(id));
            msgClient.send(new MessageBuffer(MESSAGE.CMD_CHAT_MAP_ACTION, chatMapAction.toProtobuf.array));
        }

        id = friendRepo.getChatId(to!int(u.id), to!int(u.id));

        auto user = repo.findAllByUID(u.id);
        if(user !is null)
        {
            TransportSession msgServerClient =  MsgServerClient.getInstance().getForwardClient(user.id);
            if (msgServerClient !is null)
            {
                // msgServerClient.send(new MessageBuffer(MESSAGE.CMD_AUTHORIZE_REQ,cast(ubyte[])(to!string(user.id))));
            }
            else
            {
                logWarning("no msgserver alive!");
                user = null;
            }
        }
        else
        {
            logWarning("user %s is not found in database", u.id);
            std.stdio.stdout.flush();
            return null;
        }

        auto rt =  toUser(user);
        rt.chatId = to!string(id);
        return rt;
    }

    static void InitOssInfo(AuthenticateRES msg)
    {
        SystemConfigRepository repo = new SystemConfigRepository;
        string[string] systemConfig = repo.getAll();
        msg.ossKey = systemConfig.get("oss-access-key","");
        msg.ossSecret = systemConfig.get("oss-access-secret","");
        msg.ossEndpoint = systemConfig.get("oss-endpoint","");
        msg.ossBucketName = systemConfig.get("oss-bucketname","");
    }
}
